#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <stdarg.h>
#define LOG "./log.txt"
enum{
    WARING = 1,
    ERROR,
    FATAL,
    NORMAL
};

const char* level_string(int level){
    switch (level)
    {
    case 1: return "WARING";
    case 2: return "ERROR ";
    case 3: return "FATAL ";
    case 4: return "NORMAL";
    default:    return "UNDEFINE";
    }
}

void logmessage(int level,const char* message,...){
    //[level][time]:message
    char buffer1[1024];
    time_t now;
    now = time(nullptr);
    struct tm* tm = localtime(&now);
    snprintf(buffer1,1024,"[%s][%4d-%-2d-%-2d %-2d:%-2d:%-2d]",level_string(level) \
    ,tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);

    va_list va;
    va_start(va,message);
    char buffer2[1024];
    vsnprintf(buffer2,1024,message,va);

    FILE* fd = fopen(LOG,"a");
    fprintf(fd,"%s%s\n",buffer1,buffer2);
    fclose(fd);
}